﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE.Survivor
{
    public class BulletRenderer:IClass
    {
        private BattleGameObject model;
        private BattleEffectObject effectModel;
        private Vector3 position;
        public void Initialize(BulletConfig config)
        {
            if (config.model_id > 0)
            {
                model = BattlePoolManager.Instance.Get(config.model_id);
                model.Parent = SurvivorManager.Instance.Root;
            }
            if (config.effect_model_id > 0)
            {
                effectModel = BattlePoolManager.Instance.GetEffect(config.effect_model_id);
                effectModel.Parent = SurvivorManager.Instance.Root;
            }
        }

        public void SetParent(Transform parent)
        {
            if (model != null)
            {
                model.Parent = parent;
            }
            if (effectModel != null)
            {
                effectModel.Parent = parent;
            }
        }

        public Vector3 GetPosition()
        {
            if (model != null)
            {
                return model.Position;
            }
            if (effectModel != null)
            {
                return effectModel.Position;
            }
            return position;
        }
        

        public void Update()
        {
            //model.Position = position;
        }

        public void SetPosition(Vector3 position)
        {
            this.position = position;
            if (model != null)
            {
                model.Position = position;
            }
            if (effectModel != null)
            {
                effectModel.Position = position;
            }
        }

        public void SetForward(Vector3 forward)
        {
            if (model != null)
            {
                model.Forward = forward;
            }
            if (effectModel != null)
            {
                effectModel.Forward = forward;
            }
        }

        public void Dispose()
        {
            if (model != null)
            {
                BattlePoolManager.Instance.Release(model);
                model = null;
            }
            if (effectModel != null)
            {
                BattlePoolManager.Instance.ReleaseEffect(effectModel);
                effectModel = null;
            }
        }

        public void OnReset()
        {
            model = null;
            effectModel = null;
        }
    }
}
